package com.ooono.factorytest.items;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ooono.factorytest.R;
import com.ooono.factorytest.base.BaseActivity;

import mcs8.mdm.PolicyManager;

public class KeyTestActivity extends BaseActivity {

    public static final String KEY = "key";

    private String model = "DSJ-V1PRO";

    private TextView keyPttTv;
    private TextView keyVideoTv;
    private TextView keyFnTv;
    private TextView keyCameraTv;
    private TextView keyRecordTv;
    private TextView keyMarkTv;
    private TextView keySosTv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        model = PolicyManager.getInternalProductModel();
    }

    @Override
    public int getContentLayout() {
        return R.layout.content_key_test;
    }

    @Override
    public void initViews() {
        ConstraintLayout v1KeyLl = findViewById(R.id.ll_v1_key);
        ConstraintLayout s6KeyLl = findViewById(R.id.ll_s6_key);
        ConstraintLayout m7KeyLl = findViewById(R.id.cl_m7_key);
        ConstraintLayout v9KeyLl = findViewById(R.id.cl_v9_key);
        ConstraintLayout v5KeyLl = findViewById(R.id.ll_v5_key);
        ConstraintLayout h9KeyLl = findViewById(R.id.ll_h9_key);
        if ("DSJ-S6".equals(model)) {
            v1KeyLl.setVisibility(View.GONE);
            s6KeyLl.setVisibility(View.VISIBLE);
            m7KeyLl.setVisibility(View.GONE);
            v9KeyLl.setVisibility(View.GONE);
            h9KeyLl.setVisibility(View.GONE);
            keyPttTv = findViewById(R.id.tv_s6_key_test_item_ptt);
            keyVideoTv = findViewById(R.id.tv_s6_key_test_item_video);
            keyFnTv = findViewById(R.id.tv_s6_key_test_item_fn);
            keyCameraTv = findViewById(R.id.tv_s6_key_test_item_camera);
            keyRecordTv = findViewById(R.id.tv_s6_key_test_item_record);
            keySosTv = findViewById(R.id.tv_s6_key_test_item_sos);
        } else if ("DSJ-V1PRO".equals(model)) {
            v1KeyLl.setVisibility(View.VISIBLE);
            s6KeyLl.setVisibility(View.GONE);
            m7KeyLl.setVisibility(View.GONE);
            v9KeyLl.setVisibility(View.GONE);
            h9KeyLl.setVisibility(View.GONE);
            keyPttTv = findViewById(R.id.tv_v1_key_test_item_ptt);
            keyVideoTv = findViewById(R.id.tv_v1_key_test_item_video);
            keyFnTv = findViewById(R.id.tv_v1_key_test_item_fn);
            keyCameraTv = findViewById(R.id.tv_v1_key_test_item_camera);
            keyRecordTv = findViewById(R.id.tv_v1_key_test_item_record);
            keyMarkTv = findViewById(R.id.tv_v1_key_test_item_mark);
        } else if ("DSJ-M7".equals(model)) {
            v1KeyLl.setVisibility(View.GONE);
            s6KeyLl.setVisibility(View.GONE);
            m7KeyLl.setVisibility(View.VISIBLE);
            v9KeyLl.setVisibility(View.GONE);
            h9KeyLl.setVisibility(View.GONE);
            keyCameraTv = findViewById(R.id.tv_m7_key_test_item_camera);
            keyVideoTv = findViewById(R.id.tv_m7_key_test_item_video);
            keyPttTv = findViewById(R.id.tv_m7_key_test_item_ptt);
            keyFnTv = findViewById(R.id.tv_m7_key_test_item_fn);
            keyRecordTv = findViewById(R.id.tv_m7_key_test_item_record);
            keySosTv = findViewById(R.id.tv_m7_key_test_item_sos);
            keyMarkTv = findViewById(R.id.tv_m7_key_test_item_mark);
        } else if ("DSJ-V9".equals(model) || "ST-AD100-A1P2".equals(model)) {
            v1KeyLl.setVisibility(View.GONE);
            s6KeyLl.setVisibility(View.GONE);
            m7KeyLl.setVisibility(View.GONE);
            v9KeyLl.setVisibility(View.VISIBLE);
            h9KeyLl.setVisibility(View.GONE);
            keyCameraTv = findViewById(R.id.tv_v9_key_test_item_camera);
            keyVideoTv = findViewById(R.id.tv_v9_key_test_item_video);
            keyPttTv = findViewById(R.id.tv_v9_key_test_item_ptt);
            keyFnTv = findViewById(R.id.tv_v9_key_test_item_fn);
            keyRecordTv = findViewById(R.id.tv_v9_key_test_item_record);
            keySosTv = findViewById(R.id.tv_v9_key_test_item_sos);
        } else if ("DSJ-V5".equals(model)) {
            v1KeyLl.setVisibility(View.GONE);
            s6KeyLl.setVisibility(View.GONE);
            m7KeyLl.setVisibility(View.GONE);
            v9KeyLl.setVisibility(View.GONE);
            v5KeyLl.setVisibility(View.VISIBLE);
            h9KeyLl.setVisibility(View.GONE);
            keyPttTv = findViewById(R.id.tv_v5_key_test_item_ptt);
            keyVideoTv = findViewById(R.id.tv_v5_key_test_item_video);
            keyFnTv = findViewById(R.id.tv_v5_key_test_item_fn);
            keyCameraTv = findViewById(R.id.tv_v5_key_test_item_camera);
            keyRecordTv = findViewById(R.id.tv_v5_key_test_item_record);
            keySosTv = findViewById(R.id.tv_v5_key_test_item_sos);
        } else if ("DSJ-H9".equals(model)) {
            v1KeyLl.setVisibility(View.GONE);
            s6KeyLl.setVisibility(View.GONE);
            m7KeyLl.setVisibility(View.GONE);
            v9KeyLl.setVisibility(View.GONE);
            v5KeyLl.setVisibility(View.GONE);
            h9KeyLl.setVisibility(View.VISIBLE);
            keyPttTv = findViewById(R.id.tv_h9_key_test_item_ptt);
            keyVideoTv = findViewById(R.id.tv_h9_key_test_item_video);
            keyCameraTv = findViewById(R.id.tv_h9_key_test_item_camera);
            keySosTv = findViewById(R.id.tv_h9_key_test_item_sos);
            keyRecordTv = findViewById(R.id.tv_h9_key_test_item_record);
            keyMarkTv = findViewById(R.id.tv_h9_key_test_item_mark);
        }
    }

    @Override
    public int getTestTitle() {
        return R.string.key_test_title;
    }

    @Override
    public String getTestKey() {
        return KEY;
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerKeyReceiver();
    }

    private ButtonBroadcastReceiver buttonBroadcastReceiver;

    private void registerKeyReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.setPriority(6666);
        intentFilter.addAction(ButtonBroadcastReceiver.PTT_PRESS_KEY);
        intentFilter.addAction(ButtonBroadcastReceiver.PTT_LONG_PRESS_KEY);
        intentFilter.addAction(ButtonBroadcastReceiver.PTT_DOWN_KEY);
        intentFilter.addAction(ButtonBroadcastReceiver.PTT_UP_KEY);
        intentFilter.addAction(ButtonBroadcastReceiver.RECORD_PRESS_KEY);
        intentFilter.addAction(ButtonBroadcastReceiver.VIDEO_PRESS_KEY);
        intentFilter.addAction(ButtonBroadcastReceiver.FN_PRESS_KEY);
        intentFilter.addAction(ButtonBroadcastReceiver.MARK_PRESS_KEY);
        intentFilter.addAction(ButtonBroadcastReceiver.SOS_PRESS_KEY);
        intentFilter.addAction(ButtonBroadcastReceiver.PIC_PRESS_KEY);
        intentFilter.addAction(ButtonBroadcastReceiver.BATTERY_OUT_ACTION);
        intentFilter.addAction(ButtonBroadcastReceiver.BATTERY_IN_ACTION);
        buttonBroadcastReceiver = new ButtonBroadcastReceiver();
        registerReceiver(buttonBroadcastReceiver, intentFilter);
    }

    @Override
    protected void onPause() {
        unregisterReceiver(buttonBroadcastReceiver);
        super.onPause();
    }

    class ButtonBroadcastReceiver extends BroadcastReceiver {

        private static final String PTT_DOWN_KEY = "android.intent.action.DOWN_PTT_KEY";
        private static final String PTT_UP_KEY = "android.intent.action.UP_PTT_KEY";
        private static final String PTT_PRESS_KEY = "android.intent.action.PRESS_PTT_KEY";
        private static final String PTT_LONG_PRESS_KEY = "android.intent.action.LONG_PRESS_PTT_KEY";

        private static final String RECORD_PRESS_KEY = "android.intent.action.PRESS_RECORD_KEY";

        private static final String VIDEO_PRESS_KEY = "android.intent.action.PRESS_VIDEO_KEY";

        private static final String FN_PRESS_KEY = "android.intent.action.PRESS_FN_KEY";

        private static final String MARK_PRESS_KEY = "android.intent.action.PRESS_MARK_KEY";

        private static final String SOS_PRESS_KEY = "android.intent.action.PRESS_SOS_KEY";

        private static final String PIC_PRESS_KEY = "android.intent.action.PRESS_PIC_KEY";

        private static final String BATTERY_OUT_ACTION = "android.intent.action.BATTERY_OUT";
        private static final String BATTERY_IN_ACTION = "android.intent.action.BATTERY_IN";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.d("ButtonBroadcastReceiver", "action is " + action);
            Log.d("tag_ooono", "action is " + action);
            abortBroadcast();
            Log.d("tag_ooono", ">>>---" + ", model " + Build.MODEL + ", internal model " + model);
            if (BATTERY_OUT_ACTION.equals(action)) {
                Log.d("tag_ooono", ">>>-----------BATTERY_OUT_ACTION");
            } else if (BATTERY_IN_ACTION.equals(action)) {
                Log.d("tag_ooono", ">>>-----------BATTERY_IN_ACTION");
            } else if (PTT_PRESS_KEY.equals(action)) {
                keyPttTv.setTextColor(KeyTestActivity.this.getResources().getColor(R.color.green, null));
            } else if (RECORD_PRESS_KEY.equals(action)) {
                keyRecordTv.setTextColor(KeyTestActivity.this.getResources().getColor(R.color.green, null));
            } else if (VIDEO_PRESS_KEY.equals(action)) {
                keyVideoTv.setTextColor(KeyTestActivity.this.getResources().getColor(R.color.green, null));
            } else if (FN_PRESS_KEY.equals(action)) {
                keyFnTv.setTextColor(KeyTestActivity.this.getResources().getColor(R.color.green, null));
            } else if (MARK_PRESS_KEY.equals(action)) {
                keyMarkTv.setTextColor(KeyTestActivity.this.getResources().getColor(R.color.green, null));
            } else if (SOS_PRESS_KEY.equals(action)) {
                keySosTv.setTextColor(KeyTestActivity.this.getResources().getColor(R.color.green, null));
            } else if (PIC_PRESS_KEY.equals(action)) {
                keyCameraTv.setTextColor(KeyTestActivity.this.getResources().getColor(R.color.green, null));
            }
        }
    }
}
